import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import { Role } from '../role';

import { ActivatedRoute, Params } from '@angular/router';
import { RoleService } from '../role.service';

@Component({
    selector: 'role-detail',
    templateUrl: './role-detail.component.html',
    // styleUrls: ['./role-detail.component.css']
})

export class RoleDetailComponent implements OnInit {
    @Input()
    role: Role;

    @Output() onVoted = new EventEmitter<boolean>();

    constructor(
        private roleService: RoleService,
        private router: ActivatedRoute) {

    }

    ngOnInit() {
        this.router.params.forEach((params: Params) => {
            let role_id = +params['role_id'];
            this.roleService.getRole(role_id).then(role => this.role = role);
        });
    }

    goBack() {
        window.history.back();
    }

    save(): void {
        this.roleService.save(this.role).then(() => this.cancel());
    }

    delete(): void {
        this.roleService.delete(this.role).then(() => this.cancel());
    }

    cancel(): void {
        this.role = null;
      this.onVoted.emit();
    }
}
